import socket
import datetime
import json
import tcp_add_pb2
from google.protobuf import json_format
def xlh():
    dict1 = {}
    send_name = input("姓名:")
    send_content = input("内容:")
    send_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    dict1["name"] = send_name
    dict1["time"] = send_time
    dict1["show"] = send_content
    # 将json转换为protobuf格式
    pbStringResponse = json_format.Parse(json.dumps(dict1), tcp_add_pb2.param())
    return pbStringResponse.SerializeToString()

if __name__ == '__main__':
    # 1.创建tcp端套接字
    # AF_INEF表示ipv4的地址类型
    # SOCK_STREAMM:tcp传输协议类型
    tcp_client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # 2.和服务端建立连接
    tcp_client_socket.connect(("127.0.0.1", 9910))
    while True:
        a = xlh()
        # 将数据传输
        tcp_client_socket.send(a)

        recv_data = tcp_client_socket.recv(1024)
        tmessage = tcp_add_pb2.param()
        # 将信息反序列化
        tmessage.ParseFromString(recv_data)
        m_name = tmessage.name
        m_time = tmessage.time
        m_show = tmessage.show
        print(f'姓名:{m_name}在{m_time}时说{m_show}')

    # 5.关闭套接字
    tcp_client_socket.close()